<?php
/**
 * class App_Model_Mail
 *
 * @author kim
 */
class App_Model_Mail
{
    /**
     * check email address validation
     *
     * @param string $email - the email that need to be check
     * @return boolean
     */
    static function isEmail($email)
    {
        return Zend_Validate::is($email, 'EmailAddress');
    }

    /**
     * construct
     */
    public function __construct()
    {
    }

    /**
     * send mail
     *
     * @param string $to the recipient email address
     * @param string $subject email subject
     * @param string $body email body
     * @return boolean
     */
    public function send($to, $subject = '', $body = '', $from = 'mail@dine.com')
    {
        $mail = new Zend_Mail('utf-8');
        $mail->addTo($to, $to);
        $mail->setSubject($subject);
        $mail->setBodyHtml($body);

        try {
            $mail->setFrom($from, 'team');
            if ($mail->send()) {
                return true;
            } else {
                return false;
            }
        } catch (Exception $e) {
            App_Model_Log::write($e);

            $config = array(
                'auth' => 'login',
                'username' => 'cityroof@yeah.net',
                'password' => 'greate',
            );
            $transport = new Zend_Mail_Transport_Smtp('smtp.yeah.net', $config);
            $mail->clearFrom()->setFrom('cityroof@yeah.net', 'smtp_yeah_net');
            if ($mail->send($transport)) {
                return true;
            } else {
                return false;
            }
        }
    }

    static public function notifyUser($data)
    {
        $userModel = new App_Model_User();
        $userInfo = $userModel->getUser($data['user_id'], false, false);
        if ($userInfo) {
            $to = $userInfo['email'];
            $subject = $data['mail_subject'];
            if (APPLICATION_ENV == 'production') {
                $mail = new App_Model_Mail();
                $body = $data['mail_body'];
                return $mail->send($to, $subject, $body);
            } else {
                //var_dump($data);
                return true;
            }
        } else {
            return false;
        }
    }

}